package com.zxx.mydesignpluginhost;

import androidx.appcompat.app.AppCompatActivity;
import dalvik.system.DexClassLoader;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.zxx.mydesignplugin.Constants;
import com.zxx.mydesignplugin.PluginManager;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class MainActivity extends AppCompatActivity {

    PluginManager pluginMgr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
            if (checkSelfPermission(perms[0]) == PackageManager.PERMISSION_DENIED) {
                requestPermissions(perms, 100);
            }
        }

        PluginManager.init(getApplicationContext());
        pluginMgr = PluginManager.getInstance();

        final String pluginApkPath = Environment.getExternalStorageDirectory()
                + File.separator + "plugins" + File.separator
                + "MyDesignPluginPlugin-debug.apk";

        Log.e("Tag-->", pluginApkPath + "<--");

        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {

                String pluginClazz = "com.zxx.mydesignpluginplugin.MainActivity";
                String pluginPackage = "com.zxx.mydesignpluginplugin";
                Log.e("Tag-->", "### 插件路径 : " + pluginApkPath);

                File file = new File(pluginApkPath);
                if (file.exists()) {
                    Log.e("Tag-->", "### apk存在");
                } else {
                    Log.e("Tag-->", "### no apk存在");
                }

                //String includeApk = Environment.getExternalStorageDirectory()
                //        + File.separator + "plugins" + File.separator
                //        + "IncludeDemo.apk";
                //String includeClass = "com.example.includedemo.MainActivity";
                //String includePackage = "com.example.includedemo";
                //dexTest(pluginApkPath, pluginClazz);

                PluginManager.getInstance().loadApk(pluginApkPath);
                //PluginManager.getInstance().loadApk(includeApk);

                Intent intent = new Intent();
                intent.putExtra(Constants.PLUGIN_CLASS_NAME, pluginClazz);
                intent.putExtra(Constants.PACKAGE_NAME, pluginPackage);

                pluginMgr.startActivity(intent);
            }
        }, 5000);
    }

    private void dexTest(String dexPath, String className) {
        String dexOutput = getApplicationContext().getDir("dex", 0).getAbsolutePath();
        ClassLoader classLoader = getApplicationContext().getClassLoader();
        Log.e("Tag-->", "### out : " + dexOutput + ", class loader : " + classLoader
                + " , dex path " + dexPath);

        DexClassLoader dexClassLoader = new DexClassLoader(dexPath, dexOutput, null, classLoader);

        try {
            Class<?> aClass = dexClassLoader.loadClass(className);
            Constructor<?> constructor = aClass.getConstructor(new Class[]{});
            constructor.setAccessible(true);

            Object object = constructor.newInstance(new Object[]{});

            Log.e("Tag-->", "### 插件对象 -----> : " + object);
        } catch (ClassNotFoundException | IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
